Skip to content

Conversation

@arrdem
Copy link
Collaborator

@arrdem arrdem commented Jun 10, 2025

Identified in manual testing of v1.6.0-rc0.

Because of overly conservative removals of realpath in #579, an issue is exposed where $VIRTUAL_ENV would be an un-resolved relative path both under Bazel and more significantly once a user activates a linked venv. This isn't so bad for binaries which usually don't chdir, but it's a problem for shells.

Since MacOS doesn't ship a realpath which can be configured to ignore symlinks, we can't just realpath the runfiles dir or the virtualenv home. But once we've configured a Python interpreter what we can do is use os.path.abspath. Unlike realpath, abspath does not attempt to resolve symlink path segments. It just uses normpath to eliminate relative path segments. This allows us to compute an absolute path in a portable way, once we get an appropriate interpreter up and running.


Changes are visible to end-users: no

Test plan

  • Manual testing; please provide instructions so we can reproduce:
❯ bazel run //examples/py_binary:py_binary.venv
INFO: Analyzed target //examples/py_binary:py_binary.venv (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target //examples/py_binary:py_binary.venv up-to-date:
  bazel-bin/examples/py_binary/py_binary.venv
INFO: Elapsed time: 0.785s, Critical Path: 0.02s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
INFO: Running command line: bazel-bin/examples/py_binary/py_binary.venv
Linking: /private/var/tmp/_bazel_arrdem/93bfea6cdc1153cc29a75400cd38823a/execroot/aspect_rules_py/bazel-out/darwin_arm64-fastbuild/bin/examples/py_binary/.py_binary.venv -> /Users/arrdem/Documents/work/aspect/rules_py/examples/py_binary/.py_binary.venv

To activate the virtualenv run:
    source /Users/arrdem/Documents/work/aspect/rules_py/examples/py_binary/.py_binary.venv/bin/activate

Link is up to date!

❯ source /Users/arrdem/Documents/work/aspect/rules_py/examples/py_binary/.py_binary.venv/bin/activate

❯ env | grep -e PYTHON -e RUNFILES -e VIRTUAL -e VENV | sort
PYTHONEXECUTABLE=/Users/arrdem/Documents/work/aspect/rules_py/examples/py_binary/.py_binary.venv/bin/python
PYTHONHOME=/private/var/tmp/_bazel_arrdem/93bfea6cdc1153cc29a75400cd38823a/execroot/aspect_rules_py/bazel-out/darwin_arm64-fastbuild/bin/examples/py_binary/py_binary.venv.runfiles/python_toolchain_aarch64-apple-darwin
RUNFILES_DIR=/private/var/tmp/_bazel_arrdem/93bfea6cdc1153cc29a75400cd38823a/execroot/aspect_rules_py/bazel-out/darwin_arm64-fastbuild/bin/examples/py_binary/py_binary.venv.runfiles
RUNFILES_MANIFEST_FILE=/private/var/tmp/_bazel_arrdem/93bfea6cdc1153cc29a75400cd38823a/execroot/aspect_rules_py/bazel-out/darwin_arm64-fastbuild/bin/examples/py_binary/py_binary.venv.runfiles/MANIFEST
RUNFILES_REPO_MAPPING=/private/var/tmp/_bazel_arrdem/93bfea6cdc1153cc29a75400cd38823a/execroot/aspect_rules_py/bazel-out/darwin_arm64-fastbuild/bin/examples/py_binary/py_binary.venv.runfiles/_repo_mapping
VIRTUAL_ENV_DISABLE_PROMPT=1
VIRTUAL_ENV=/Users/arrdem/Documents/work/aspect/rules_py/examples/py_binary/.py_binary.venv

@arrdem arrdem added this to the 1.6.0 milestone Jun 10, 2025
@arrdem arrdem force-pushed the arrdem/venv-abspath branch from c27399b to ac3395f Compare June 10, 2025 00:27
@aspect-workflows
Copy link

aspect-workflows bot commented Jun 10, 2025

Test

2 test targets passed

Targets
//py/tests/py_venv_image_layer:my_app_amd64_layers_test [k8-fastbuild]                 30ms
//py/tests/py_venv_image_layer:my_app_arm64_layers_test [k8-fastbuild]                 64ms

Total test execution time was 94ms. 38 tests (95.0%) were fully cached saving 1m 9s.

@github-actions
Copy link

github-actions bot commented Jun 10, 2025

e2e/use_release folder: LCOV of commit 716c463 during CI #1864

Summary coverage rate:
  lines......: 100.0% (2 of 2 lines)
  functions..: 100.0% (1 of 1 function)
  branches...: no data found

Files changed coverage rate: n/a

@arrdem arrdem force-pushed the arrdem/venv-abspath branch from 963dbae to a66c3f6 Compare June 10, 2025 18:07
@arrdem arrdem enabled auto-merge (squash) June 10, 2025 18:24
@arrdem arrdem merged commit 5f6c518 into main Jun 10, 2025
16 checks passed
@arrdem arrdem deleted the arrdem/venv-abspath branch June 10, 2025 18:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants